ImapFolderAgent now recognizes "true"/"false" as boolean option values.

Add a utility method Agent#boolify to make it easier to handle boolean
option values.

Akinori MUSHA 10 lat temu
rodzic
commit
045fb957b2

+ 13 - 0
app/models/agent.rb

@@ -225,6 +225,19 @@ class Agent < ActiveRecord::Base
225 225
     # Implement me in your subclass to test for valid options.
226 226
   end
227 227
 
228
+  # Utility Methods
229
+
230
+  def boolify(option_value)
231
+    case option_value
232
+    when true, 'true'
233
+      true
234
+    when false, 'false'
235
+      false
236
+    else
237
+      nil
238
+    end
239
+  end
240
+
228 241
   # Class Methods
229 242
 
230 243
   class << self

+ 5 - 6
app/models/agents/imap_folder_agent.rb

@@ -139,9 +139,7 @@ module Agents
139 139
 
140 140
       %w[ssl mark_as_read].each { |key|
141 141
         if options[key].present?
142
-          case options[key]
143
-          when true, false
144
-          else
142
+          if boolify(options[key]).nil?
145 143
             errors.add(:base, '%s must be a boolean value' % key)
146 144
           end
147 145
         end
@@ -204,7 +202,7 @@ module Agents
204 202
               end
205 203
             }
206 204
           when 'has_attachment'
207
-            case value
205
+            case boolify(value)
208 206
             when true, false
209 207
             else
210 208
               errors.add(:base, 'conditions.%s must be a boolean value or null' % key)
@@ -260,7 +258,7 @@ module Agents
260 258
               }
261 259
             }
262 260
           when 'has_attachment'
263
-            value == mail.has_attachment?
261
+            boolify(value) == mail.has_attachment?
264 262
           else
265 263
             log 'Unknown condition key ignored: %s' % key
266 264
             true
@@ -294,7 +292,7 @@ module Agents
294 292
           notified << mail.message_id if mail.message_id
295 293
         end
296 294
 
297
-        if interpolated['mark_as_read']
295
+        if boolify(interpolated['mark_as_read'])
298 296
           log 'Marking as read'
299 297
           mail.mark_as_read
300 298
         end
@@ -303,6 +301,7 @@ module Agents
303 301
 
304 302
     def each_unread_mail
305 303
       host, port, ssl, username = interpolated.values_at(:host, :port, :ssl, :username)
304
+      ssl = boolify(ssl)
306 305
 
307 306
       log "Connecting to #{host}#{':%d' % port if port}#{' via SSL' if ssl}"
308 307
       Client.open(host, Integer(port), ssl) { |imap|

+ 12 - 4
spec/models/agents/imap_folder_agent_spec.rb

@@ -118,11 +118,19 @@ describe Agents::ImapFolderAgent do
118 118
       end
119 119
 
120 120
       it 'should validate the boolean fields' do
121
-        @checker.options['ssl'] = false
122
-        @checker.should be_valid
121
+        %w[ssl mark_as_read].each do |key|
122
+          @checker.options[key] = 1
123
+          @checker.should_not be_valid
123 124
 
124
-        @checker.options['ssl'] = 'true'
125
-        @checker.should_not be_valid
125
+          @checker.options[key] = false
126
+          @checker.should be_valid
127
+
128
+          @checker.options[key] = 'true'
129
+          @checker.should be_valid
130
+
131
+          @checker.options[key] = ''
132
+          @checker.should be_valid
133
+        end
126 134
       end
127 135
 
128 136
       it 'should validate regexp conditions' do